class Room {
  constructor() {
    this.roomList = {};
    this.roomMaxPlayerNum = global.option.roomMaxPlayerNum;
  }

  create(player, status) {
    let key = global.Core.util.randomStr(global.option.roomKeyNum);
    while (this.roomList[key]) {
      key = global.Core.util.randomStr(global.option.roomKeyNum);
    }
    this.roomList[key] = {
      status, // 是否为私密
      playerList: [], // 所有的玩家
      master: player.socket.id, // 房主
      maxPlayerNum: this.roomMaxPlayerNum,
      play: false, // 是否开始了游戏
      key
    };
    // 创建成功后发送给客户端
    player.emit('room.create');
    global.Core.socket.system(`${player.data.username}创建了房间`, 'green', !status && player.socket);
    this.add(key, player, true);
    this.sendRoomInfo(key);
  }

  get(key) {
    if (!this.roomList[key]) {
      global.Core.socket.system(`房间不存在。`, 'red', null, key);
      return;
    }
    return this.roomList[key];
  }

  add(key, player, flag) {
    // 判断key是否正确，是否有这个房间
    if (key.length != 10) {
      player.emit('room.add.error');
      global.Core.socket.system('密令错误', null, player.socket);
      return;
    }
    let room = this.get(key);
    if (!room) {
      global.Core.socket.system(`房间不存在。`, 'red', player.socket);
      player.emit('room.add.error');
      return;
    }
    if (room.isGame) {
      player.emit('room.add.error');
      global.Core.socket.system('这个房间已经开始游戏了，无法加入哟', null, player.socket);
      player.emit('jump', {
        view: 'room'
      });
      return;
    }
    if (room.playerList.length >= this.roomMaxPlayerNum) {
      player.emit('room.add.error');
      global.Core.socket.system('房间人数已满', null, player.socket);
      player.emit('jump', {
        view: 'room'
      });
      return;
    }

    // 给玩家设置在房间准备的属性
    if (room.key == player.data.room) {
      global.Core.socket.system('你已经在这个房间中了', null, player.socket);
      return;
    } else {
      player.set('ready', 0);
      player.set('room', room.key);
      player.socket.join(room.key);
      room.playerList.push(player.data);
    }

    !flag && global.Core.socket.system(`${player.data.username}加入了房间`, 'green', null, key);
    this.sendRoomInfo(key);
  }

  sendRoomInfo(key) {
    global.Core.socket.emit('room.add', {
      roomInfo: this.get(key),
    }, null, key);
  }

  leave(player, flag) {
    // 找到这个房间
    let room = this.get(player.data.room);
    player.emit('room.leave');

    if (!room) {
      // 房间不存在
      return;
    } else {
      room.playerList = room.playerList.filter(i => i.id != player.socket.id);
      !flag && global.Core.socket.system(`${player.data.username}退出了房间`, 'white', player.socket, player.data.room);

      // 判断是否为房主
      if (room.master == player.socket.id) {
        // 查看房间是否还有玩家，如果有将房主的位置交给他
        if (room.playerList.length > 0) {
          room.master = room.playerList[0].id;
          let newMaster = global.Core.player.get(room.master);
          global.Core.socket.system(`你被系统评为优秀玩家，所以房主就交给你了`, 'green', newMaster.socket);
        } else {
          // 没有玩家了就将这个房间销毁
          this.destroy(player.data.room);
          room.status && global.Core.socket.system(`${player.data.username}的房间没人玩，自动销毁了`);
          return;
        }
      }
    }

    player.socket.leave(player.data.room);
    this.sendRoomInfo(player.data.room);
    player.delete('room');
  }

  destroy(key) {
    delete this.roomList[key];
    global.Core.socket.emit('jump', {
      view: 'home'
    }, null, key);
    global.Core.player.destroyRoom(key);
  }
}

module.exports = Room;